// CPage3.cpp : Implementierungsdatei
//

#include "stdafx.h"
#include "Charplan.h"
#include "CPage3.h"
#include "charplandoc.h"
#include ".\cpage3.h"


// CPage3-Dialogfeld

IMPLEMENT_DYNAMIC(CPage3, CAPage)
CPage3::CPage3(CWnd* pParent /*=NULL*/)
	: CAPage(CPage3::IDD, pParent)
{
}

CPage3::~CPage3()
{
}

void CPage3::DoDataExchange(CDataExchange* pDX)
{
	CAPage::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST1, mwnd_liste);
	DDX_Control(pDX, IDC_CHECK1, mwnd_show_all_spells);
	DDX_Control(pDX, IDC_CHECK2, mwnd_only_top);
	DDX_Control(pDX, IDC_CHECK3, mwnd_usetypeasname);
}


BEGIN_MESSAGE_MAP(CPage3, CAPage)
	ON_WM_SIZE()
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST1, OnNMCustomdrawList1)
	ON_BN_CLICKED(IDC_CHECK1, OnBnClickedCheck1)
	ON_BN_CLICKED(IDC_CHECK2, OnBnClickedCheck2)
	ON_BN_CLICKED(IDC_CHECK3, OnBnClickedCheck3)
END_MESSAGE_MAP()


void CPage3::OnBnClickedCheck1()
{
	GetDocument()->SetOption(_T("ShowAllSpells"),mwnd_show_all_spells.GetCheck());
	OnClassChanged();
}

void CPage3::OnBnClickedCheck2()
{
	GetDocument()->SetOption(_T("ShowOnlyTopSpells"),mwnd_only_top.GetCheck());

	if (mwnd_only_top.GetCheck())
	{
		mwnd_usetypeasname.ShowWindow(SW_SHOW);
		mwnd_usetypeasname.RedrawWindow();
	}
	else
	{
		mwnd_usetypeasname.SetCheck(0);
		mwnd_usetypeasname.ShowWindow(SW_HIDE);
	}

	OnClassChanged();

}

void CPage3::OnBnClickedCheck3()
{
	GetDocument()->SetOption(_T("UseSpellTypeAsName"),mwnd_usetypeasname.GetCheck());
	OnClassChanged();
}



void CPage3::OnLevelChanged()
{
	if (!mwnd_show_all_spells.GetCheck())
		OnClassChanged();
}

void CPage3::OnRRChanged()
{
    // only needed for "poison" skill
	OnClassChanged();
}

// CPage3-Meldungshandler
void CPage3::OnClassChanged()
{
	bool b_show_speclinetitle = true;
	BOOL show_all = mwnd_show_all_spells.GetCheck();
	int level;

	if (IsWindowVisible()) SetRedraw(false);
	mwnd_liste.DeleteAllItems();

	CStringArray spelllines;
	CStringArray spellcats;
	CStringArray spells;
	IntArray	 spell_levels;
	IntArray	 spell_ids;
	CStringArray properties_names;
	CStringArray properties_vals;

	gdbinterface.GetListOfSpellLines(spelllines);


	for (int t=0;t<spelllines.GetCount();t++)
	{
		// base
		level = (int)(GetDocument()->GetLevel());
//		if (show_all) level = 50;

		gdbinterface.SelectSpellLine(spelllines[t]);

		mwnd_liste.InsertItem(LVIF_TEXT|LVIF_PARAM,
								mwnd_liste.GetItemCount(),spelllines[t],
								0,0,0,1);

		gdbinterface.GetListOfSpellCatsBase(spellcats);

		if (spellcats.GetCount() == 0)	b_show_speclinetitle = false;

		for (int t2=0;t2<spellcats.GetCount();t2++)
		{
			gdbinterface.SelectSpellCatBase(spellcats[t2]);
			gdbinterface.GetListOfSpells(&spells,&spell_levels,&spell_ids);

			// Check if something to display
			int nofspells;
			for (nofspells=0;nofspells<spells.GetCount();nofspells++)
				if (spell_levels[nofspells]<=level || show_all) break;
			if (nofspells>=spells.GetCount()) continue;



			int item = mwnd_liste.InsertItem(LVIF_TEXT|LVIF_PARAM,
									mwnd_liste.GetItemCount(),spellcats[t2],
									0,0,0,2);



			int minlevel = 0;
			if (mwnd_only_top.GetCheck())
			{
				int min2 = 0;
				// find spell
				for (int t3=0;t3<spells.GetCount();t3++)
					if (spell_levels[t3]<=level)
					{
						if (minlevel<spell_levels[t3])
						{
							min2 = minlevel;
							minlevel = spell_levels[t3];
						}
					}

				switch (gdbinterface.GetSpellCatDisplayFlag())
				{
					case 0: break; // nrl
					case 1: minlevel = min2; break;// top2
					case 2: minlevel = 0; break; // all
				}
			}



			for (int t3=0;t3<spell_ids.GetCount();t3++)
			{
				if ((show_all || spell_levels[t3]<=level) && spell_levels[t3]>=minlevel)
				{
					gdbinterface.SelectSpellID(spell_ids[t3]);
					gdbinterface.GetSpellDescription(properties_names,properties_vals);

					if (mwnd_usetypeasname.GetCheck()) 
						mwnd_liste.SetItemData(item,0); // reset color
					else
					{
						 item = mwnd_liste.InsertItem(mwnd_liste.GetItemCount(),spells[t3]);
						 if (spell_levels[t3]>level)
							 mwnd_liste.SetItemData(item,4);
					}
					
					CString temp; temp.Format(_T("%i"),spell_levels[t3]);
					mwnd_liste.SetItem(item,1,LVIF_TEXT,temp,0,0,0,NULL);

					for (int t4=2;t4<properties_names.GetCount();t4++)
					{
						mwnd_liste.SetItem(item,t4,LVIF_TEXT,properties_vals[t4],0,0,0,NULL);
					}
				}
			}
		}



		// Spec
		level = GetDocument()->GetSpecLevel(spelllines[t]);
		//if (show_all) level = 50;

		if (b_show_speclinetitle)
			mwnd_liste.InsertItem(LVIF_TEXT|LVIF_PARAM,
								mwnd_liste.GetItemCount(),
								gdbinterface.GetSpellLineNameSpec(),
								0,0,0,3);

        // Poison special
        int over_cap = level;   // + rr
        int over_cap2 = level;  // + rr + items
    	if (spelllines[t] == "Vergiften" || spelllines[t] == "Player poisons" 
            || spelllines[t] == "Envenom" )
	    {
            over_cap += GetDocument()->GetRR()/10;
            over_cap2 = over_cap +(int)(GetDocument()->GetLevel())/5 + 1;
	    }  


		gdbinterface.GetListOfSpellCatsSpec(spellcats);
		for (int t2=0;t2<spellcats.GetCount();t2++)
		{
			gdbinterface.SelectSpellCatSpec(spellcats[t2]);
			gdbinterface.GetListOfSpells(&spells,&spell_levels,&spell_ids);

			// Check if something to display
			int nofspells;
			for (nofspells=0;nofspells<spells.GetCount();nofspells++)
				if (spell_levels[nofspells]<=level || show_all) break;
			if (nofspells>=spells.GetCount()) continue;


			int item = mwnd_liste.InsertItem(LVIF_TEXT|LVIF_PARAM,
									mwnd_liste.GetItemCount(),spellcats[t2],
									0,0,0,2);


			int minlevel = 0;
			if (mwnd_only_top.GetCheck())
			{
				int min2 = 0;
				// find spell
				for (int t3=0;t3<spells.GetCount();t3++)
					if (spell_levels[t3]<=over_cap2)
					{
						if (minlevel<spell_levels[t3])
						{
							min2 = minlevel;
							minlevel = spell_levels[t3];
						}
					}

				switch (gdbinterface.GetSpellCatDisplayFlag())
				{
					case 0: break; // nrl
					case 1: minlevel = min2; break;// top2
					case 2: minlevel = 0; break; // all
				}
			}


			for (int t3=0;t3<spells.GetCount();t3++)
			{
				if ((show_all || spell_levels[t3]<=over_cap2) && spell_levels[t3]>=minlevel)
				{
					gdbinterface.SelectSpellID(spell_ids[t3]);
					gdbinterface.GetSpellDescription(properties_names,properties_vals);

					if (mwnd_usetypeasname.GetCheck()) 
						mwnd_liste.SetItemData(item,0); // reset color
					else
					{
						item = mwnd_liste.InsertItem(mwnd_liste.GetItemCount(),spells[t3]);
                        {
                            // set color
						    if (spell_levels[t3]>over_cap2)     mwnd_liste.SetItemData(item,4);
                            else    
                            if (spell_levels[t3]>over_cap)      mwnd_liste.SetItemData(item,5);
                            else
						    if (spell_levels[t3]>level)	        mwnd_liste.SetItemData(item,6);
                        }
					}
					
					CString temp; temp.Format(_T("%i"),spell_levels[t3]);
					mwnd_liste.SetItem(item,1,LVIF_TEXT,temp,0,0,0,NULL);

					for (int t4=2;t4<properties_names.GetCount();t4++)
					{
						mwnd_liste.SetItem(item,t4,LVIF_TEXT,properties_vals[t4],0,0,0,NULL);
					}
				}
			}
		}
	}

	for (int t=0;t<mwnd_liste.GetHeaderCtrl()->GetItemCount();t++)
		mwnd_liste.SetColumnWidth(t,LVSCW_AUTOSIZE);

	SetRedraw(true);
	mwnd_liste.Invalidate();
}



BOOL CPage3::OnInitDialog()
{
	CAPage::OnInitDialog();

	mwnd_liste.InsertColumn(0,_T("Name"));
	mwnd_liste.InsertColumn(1,_T("Level"));
	mwnd_liste.InsertColumn(2,_T("Target"));
	mwnd_liste.InsertColumn(3,_T("Cast/Duration/Recast"));
	mwnd_liste.InsertColumn(4,_T("Range/Radius"));
	mwnd_liste.InsertColumn(5,_T("Cost"));
	mwnd_liste.InsertColumn(6,_T("Effect"));

	mwnd_liste.SetExtendedStyle(LVS_EX_FULLROWSELECT );


	if (GetDocument()->GetOption(_T("ShowAllSpells")))
		mwnd_show_all_spells.SetCheck(true);

	if (GetDocument()->GetOption(_T("ShowOnlyTopSpells")))
		mwnd_only_top.SetCheck(true);

	if (GetDocument()->GetOption(_T("UseSpellTypeAsName")))
		mwnd_usetypeasname.SetCheck(true);

	mwnd_usetypeasname.ShowWindow(mwnd_only_top.GetCheck()?SW_SHOW:SW_HIDE);


	OnClassChanged();
	Invalidate();

	return TRUE;
}

void CPage3::OnSize(UINT nType, int cx, int cy)
{
	CAPage::OnSize(nType, cx, cy);

	
	if (IsWindow(mwnd_liste.m_hWnd))
	{
		CRect rect;
		mwnd_liste.GetWindowRect(rect);
		ScreenToClient(rect);
		mwnd_liste.SetWindowPos(NULL, 0,0,
									cx-2*rect.left,cy-2*rect.top,
									SWP_NOZORDER | SWP_NOACTIVATE |SWP_NOMOVE  );

	}
}

void CPage3::OnNMCustomdrawList1(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMLVCUSTOMDRAW  lplvcd = (LPNMLVCUSTOMDRAW)pNMHDR;

	*pResult = CDRF_DODEFAULT;
	switch(lplvcd->nmcd.dwDrawStage)
	{
		case CDDS_PREPAINT:
			*pResult = CDRF_NOTIFYITEMDRAW;          // ask for item notifications.
			break;

		case CDDS_ITEMPREPAINT:
			*pResult = CDRF_DODEFAULT;

			switch (lplvcd->nmcd.lItemlParam)
			{
				case 1:
					lplvcd->clrTextBk = RGB(196, 196, 255);
					break;
				case 2:
					lplvcd->clrTextBk = RGB(212, 212, 212);
					break;
				case 3:
					lplvcd->clrTextBk = RGB(232, 232, 255);
					break;
				case 4:
   					lplvcd->clrText = RGB(164,164,164);
					break;
				case 5:
					lplvcd->clrText = RGB(128, 128, 196);
					break;
				case 6:
 					lplvcd->clrText = RGB(32, 32, 232);
					break;
			}
			break;
	}
}



